package com.moyuaninfo.myyserver.framework.utils;

import com.moyuaninfo.myyserver.framework.enums.JsonResultEnum;
import com.moyuaninfo.myyserver.framework.json.JsonResult;

/**
 * @author 祥霸
 * @date 2019/8/21
 */
public class JsonResultUtils {

    /**
     * <p> show TODO 请求成功</p>
     * @author 祥霸
     * @date 8:46 2019/8/21
     * @param data: 返回的数据
     * @return com.example.springboot.biz.json.JsonResult<T>
     */
    public static <T> JsonResult<T> success(T data) {
        return dueResult(data, JsonResultEnum.SUCCESS);
    }
    public static <T> JsonResult<T> success(T data, String msg) {
        return dueResult(data, JsonResultEnum.SUCCESS, msg);
    }

    /**
     * <p> show TODO 请求异常 </p>
     * @author 祥霸
     * @date 8:54 2019/8/21
     * @param data: 返回的数据
     * @return com.example.springboot.biz.json.JsonResult<T>
     */
    public static <T> JsonResult<T> fail(T data) {
        return dueResult(data, JsonResultEnum.FAIL);
    }
    public static <T> JsonResult<T> fail(T data, String errorMsg) {
        return dueResult(data, JsonResultEnum.FAIL, errorMsg);
    }

    /**
     * <p> show TODO 请求失败</p>
     * @author 祥霸
     * @date 8:54 2019/8/21
     * @param jsonResultEnum: 返回的状态码
     * @return com.example.springboot.biz.json.JsonResult<T>
     */
    public static <T> JsonResult<T> make(T data, JsonResultEnum jsonResultEnum) {
        return dueResult(data, jsonResultEnum);
    }
    public static <T> JsonResult<T> make(T data, JsonResultEnum jsonResultEnum, String msg) {
        return dueResult(data, jsonResultEnum, msg);
    }

    /**
     * <p> show TODO 封装返回model </p>
     * @author 祥霸
     * @date 8:55 2019/8/21
     * @param data: 返回的数据
     * @param jsonResultEnum: 返回的状态码
     * @return com.example.springboot.biz.json.JsonResult<T>
     */
    private static <T>JsonResult<T> dueResult(T data, JsonResultEnum jsonResultEnum) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(jsonResultEnum.getCode());
        result.setMsg(jsonResultEnum.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * <p> show TODO 封装返回model </p>
     * @author 祥霸
     * @date 8:55 2019/8/21
     * @param data: 返回的数据
     * @param jsonResultEnum: 返回的状态码
     * @return com.example.springboot.biz.json.JsonResult<T>
     */
    private static <T>JsonResult<T> dueResult(T data, JsonResultEnum jsonResultEnum, String errorMsg) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(jsonResultEnum.getCode());
        result.setMsg(errorMsg);
        result.setData(data);
        return result;
    }
}

